package com.atguigu.gulimall.member.controller;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author chengzhaoyong
 * @email 1658020255@qq.com
 * @date 2021-07-31 00:21:46
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    @Autowired
    CouponFeignService couponFeignService;

    //微博 登录功能 包含注册
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) throws Exception {
      MemberEntity memberEntity =  memberService.loginWeibo(socialUser);
      if(memberEntity!=null){
          return R.ok().put("member",memberEntity);
      }else{
          return R.error(BizCodeEnume.LOGIN_VALID_EXCEPTION.getCode(), BizCodeEnume.LOGIN_VALID_EXCEPTION.getMsg());


      }
    }

    @PostMapping("/auth/save")
    //@RequiresPermissions("member:member:save")
    public R saveAuth(@RequestBody MemberEntity memberEntity){
        memberService.save(memberEntity);
        return R.ok();
    }

    //查找是否有某个微信id的用户，判断是否第一次扫码登录

    @GetMapping("/openId")
    public R openId(String openId){
       MemberEntity memberEntity = memberService.openId(openId);
       if(memberEntity!=null){
           return R.ok().put("member",memberEntity);
       }else{
          return R.error(BizCodeEnume.OPEM_ID_VALID_EXCEPTION.getCode(),BizCodeEnume.OPEM_ID_VALID_EXCEPTION.getMsg());
       }

    }


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){
      MemberEntity memberEntity =  memberService.login(memberLoginVo);
      if(memberEntity!=null){
          return R.ok().put("member",memberEntity);
      }else{
          return R.error(BizCodeEnume.LOGIN_VALID_EXCEPTION.getCode(),BizCodeEnume.LOGIN_VALID_EXCEPTION.getMsg());
      }


    }


    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo){
        try{
            memberService.regist(memberRegistVo);
        }catch (UsernameExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }catch (PhoneExistException e){
            return R.error(BizCodeEnume.PHON_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHON_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }




    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("会员昵称张三");
        R membercoupons = couponFeignService.membercoupons();//假设张三去数据库查了后返回了张三的优惠券信息

        //打印会员和优惠券信息
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }
// ---------------------------
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
